package LeetCode.leetcode59;

public class Review3 {
    public int[][] generateMatrix(int n) {
        int p = 1;
        int l = 0;
        int t = 0;
        int r = n - 1;
        int b = n - 1;
        int[][] res = new int[n][n];
        while (p <= n * n) {
            int top = t;
            int bottom = b;
            int left =l;
            int right = r;
            //独有的一个
            if (p == n * n){
                res[top][left] = p++;
            }
            //上朝右
            while (left < r){
                res[t][left] = p++;
                left++;
            }
            //右朝下
            while (top < b){
                res[top][r] = p++;
                top++;
            }
            //下朝左
            while (right > l){
                res[b][right] = p++;
                right--;
            }
            //左朝上
            while (bottom > t){
                res[bottom][l] = p++;
                bottom--;
            }
            l++;
            b--;
            r--;
            t++;
        }
        return res;
    }
}
